using UnityEngine;

namespace NodeCanvas.StateMachines
{
	[AddComponentMenu("")]
	[Name("FSM")]
	[Category("Nested")]
	[Description("Execute a nested FSM OnEnter and Stop that FSM OnExit. This state is Finished when the nested FSM is finished as well")]
	public class FSMNestedFSMState : FSMState, INestedNode
	{
		[SerializeField]
		private FSM _nestedFSM;

		private bool instantiated;

		private FSM nestedFSM
		{
			get
			{
				return _nestedFSM;
			}
			set
			{
				_nestedFSM = value;
			}
		}

		[HideInInspector]
		public Graph nestedGraph
		{
			get
			{
				return nestedFSM;
			}
			set
			{
				nestedFSM = (FSM)value;
			}
		}

		protected override void Init()
		{
			CheckInstance();
		}

		protected override void Enter()
		{
			if (!nestedFSM)
			{
				Finish(false);
				return;
			}
			CheckInstance();
			nestedFSM.StartGraph(base.graphAgent, base.graphBlackboard, base.Finish);
		}

		protected override void Exit()
		{
			if ((bool)nestedFSM && nestedFSM.isRunning)
			{
				nestedFSM.StopGraph();
			}
		}

		protected override void Pause()
		{
			if ((bool)nestedFSM)
			{
				nestedFSM.PauseGraph();
			}
		}

		private void CheckInstance()
		{
			if (!instantiated && nestedFSM != null && nestedFSM.transform.parent != base.graph.transform)
			{
				nestedFSM = Object.Instantiate(nestedFSM, base.transform.position, base.transform.rotation);
				nestedFSM.transform.parent = base.graph.transform;
				instantiated = true;
			}
		}
	}
}
